package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;






import utilClasses.ListaMapa;
import utilClasses.uMapa;

public class daoMapa {

	public static ListaMapa sveMape() throws SQLException{
		Connection c = daoKonekcija.dajKonekciju();
		ListaMapa rez=new ListaMapa();
		String iskaz = "SELECT * FROM googlemapa";
		
			try {
				Statement stmt = c.createStatement();
				ResultSet rs=stmt.executeQuery(iskaz);
				while (rs.next()) {
					String id=rs.getString(1);
					String lat=rs.getString(2);
					String lgt=rs.getString(3);
					String zoom=rs.getString(4);
				    rez.getMaps().add(new uMapa(id,lat,lgt,zoom)); 
				    }
				rs.close();
				stmt.close();
				c.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				if(c!=null)c.close();
				e.printStackTrace();
			}
			return rez;
	}
	public static void insertGoogleMapa( double Gsirina, double Gduzina, double Zoom) throws SQLException {
		Connection c = daoKonekcija.dajKonekciju();
	    CallableStatement cStmt = c.prepareCall("{CALL Insert_GoogleMapa(?,?,?)}");
	    cStmt.setDouble(1, Gsirina);
	    cStmt.setDouble(2, Gduzina);
	    cStmt.setDouble(3, Zoom);
	    
	    cStmt.execute();
	
	    System.out.println("Uspjesno unesena mapa!\n");
	    cStmt.close();
	    c.close();
	}

	public static void updateMapa ( int idGoogleMapa, double GSirina, double Gduzina, double Zoom)
	    throws SQLException  { 
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c.prepareCall("{CALL updateGoogleMapa(?,?,?,?)}");
	    cStmt.setInt(1, idGoogleMapa);
	    cStmt.setDouble(2, GSirina);
	    cStmt.setDouble(3, Gduzina);
	    cStmt.setDouble(4, Zoom);
		cStmt.executeUpdate();
	System.out.println("Uspjesno promijenjen parametar!\n");
	cStmt.close();
	c.close();
	}

	public static void deleteMapa (int id ) 
		throws SQLException  { 
		
		Connection c = daoKonekcija.dajKonekciju();
		CallableStatement cStmt = c.prepareCall("{CALL deleteGoogleMapa(?)}");
	
		cStmt.setInt(1, id);
		cStmt.executeUpdate();
		System.out.println("Uspjesno obrisana mapa!\n");
		cStmt.close();
		c.close();
	}

}
